package com.eian.boot.json.config;

import com.eian.boot.json.handler.BigNumberSerializer;
import com.eian.boot.json.handler.CustomDateDeserializer;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.TimeZone;

/**
 * jackson 配置
 *
 * @author alex.meng
 */
@Slf4j
@AutoConfiguration(before = org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration.class)
public class JacksonAutoConfiguration {

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer customizer() {
        return builder -> {
            builder.configure(objectMapper());
            log.info("Jackson 初始化完成");
        };
    }

    /**
     * @author mengjiao
     * @createTime 2025-09-10
     * @description ObjectMapper配置
     */
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();

        //=== 1.支持JDK8的日期时间API（LocalDateTime等）
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        javaTimeModule.addSerializer(Long.class, BigNumberSerializer.INSTANCE);
        javaTimeModule.addSerializer(Long.TYPE, BigNumberSerializer.INSTANCE);
        javaTimeModule.addSerializer(BigInteger.class, BigNumberSerializer.INSTANCE);
        javaTimeModule.addSerializer(BigDecimal.class, ToStringSerializer.instance);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(formatter));
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(formatter));
        javaTimeModule.addDeserializer(Date.class, new CustomDateDeserializer());
        objectMapper.registerModule(javaTimeModule);

        //=== 2.支持JDK8的其他类型（Optional等）
        objectMapper.registerModule(new Jdk8Module());

        //=== 3.支持通过构造函数参数名称进行反序列化（改善POJO的反序列化）
        objectMapper.registerModule(new ParameterNamesModule());

        //=== 4.日期时间全局配置
        objectMapper.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));

        //=== 5.其他提高健壮性和兼容性的配置
        // 反序列化时，忽略JSON中存在但Java对象中没有的属性（避免解析失败）
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        // 反序列化时，遇到空字符串("")对于原始类型（如int）则设为null，防止NPE
        objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
        // 序列化时，仅包含非null值（使JSON输出更简洁）
        // objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); // 按需开启
        return objectMapper;
    }
}
